package com.jxau.mall.user.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxau.mall.common.enums.ResponseEnum;
import com.jxau.mall.user.entity.UserInfo;
import com.jxau.mall.user.mapper.UserInfoMapper;
import com.jxau.mall.user.mapper.UserMapper;
import com.jxau.mall.user.service.IUserService;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.stereotype.Service;

/**
 * @program: shop
 * @description: 用户登录业务的实现类
 * @author: guojiani
 * @create: 2025-04-24 17:27
 **/
@Service
public class UserServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserService {
    @Override
    public UserInfo login(String username, String password) throws Exception {
        // 按用户名查询用户
        LambdaQueryChainWrapper<UserInfo> queryChainWrapper = new LambdaQueryChainWrapper<>(baseMapper);
        UserInfo userInfo = queryChainWrapper.eq(UserInfo::getUsername, username)
                .eq(UserInfo::getStatus,0)
                .one();
        // 判断密码是否正确
        if(userInfo != null){
            if(BCrypt.checkpw(password,userInfo.getPassword())){
                return userInfo;
            }else {
                throw new Exception(ResponseEnum.USER_PASSWORD_ERROR.getMessage());
            }

        }else {
            throw new Exception(ResponseEnum.USER_ACCOUNT_FORBIDDEN.getMessage());
        }
    }

}